package lib.bone.flow.service.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.utils.JsonUtil;
import lib.bone.flow.dao.FlowTaskDAO;
import lib.bone.flow.domain.dto.FlowParam;
import lib.bone.flow.domain.dto.FlowTaskQueryByDTO;
import lib.bone.flow.domain.entity.FlowTask;
import lib.bone.flow.service.FlowTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: kindear
 */
@Slf4j
@Service
public class FlowTaskServiceImpl implements FlowTaskService {
    @Autowired
    FlowTaskDAO flowTaskDAO;


    @Override
    public void deleteList(List<String> ids) {
        flowTaskDAO.deleteByIdIn(ids);
    }

    @Async("threadExecutor")
    @Override
    public void buildServiceName(FlowTask task) {
        String flowParams = task.getFlowParam();
        FlowParam param = JsonUtil.string2Obj(flowParams,FlowParam.class);
        assert param != null;
        String serviceName = param.getServiceName();
        task.setServiceName(serviceName);
        log.info("更新serivceName:{}",serviceName);
        flowTaskDAO.save(task);
    }

    @Override
    public List<FlowTask> getAll() {
        return flowTaskDAO.findAll();
    }

    @Override
    public void deleteFlowTasks(String flowWorkId) {
        flowTaskDAO.deleteByFlowWorkId(flowWorkId);
    }

    @Override
    public List<FlowTask> getFlowTasks(String flowWorkId, String taskStatus) {
        return flowTaskDAO.findByFlowWorkIdAndTaskStatus(flowWorkId,taskStatus);
    }

    @Override
    public Long countTaskStatus(String taskStatus, String actUserId) {
        if(Validator.isEmpty(actUserId)){
            return flowTaskDAO.countByTaskStatus(taskStatus);
        }else {
            return flowTaskDAO.countByActUserIdAndTaskStatus(actUserId,taskStatus);
        }
    }

    @Override
    public Optional<FlowTask> findByActUserIdAndServiceId(String actUserId, String serviceId) {
        return flowTaskDAO.findByActUserIdAndServiceId(actUserId,serviceId);
    }

    @Override
    public ResponseDTO getList(Pageable pageable, Example<FlowTask> example) {
        Page<FlowTask> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = flowTaskDAO.findAll(pageable);
        }else {
            page = flowTaskDAO.findAll(example,pageable);
        }
        List<FlowTask> list = page.getContent();
        PageResultDTO<FlowTask> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public FlowTask saveOne(FlowTask flowTask) {
        return flowTaskDAO.save(flowTask);
    }

    @Override
    public Optional<FlowTask> getById(String taskId) {
        return flowTaskDAO.findById(taskId);
    }

    @Override
    public List<FlowTask> saveList(List<FlowTask> tasks) {
        return flowTaskDAO.saveAll(tasks);
    }

    @Override
    public List<FlowTask> getByFlowWorkIdAndFlowNodeId(String flowWorkId, String flowNodeId) {
        return flowTaskDAO.findByFlowWorkIdAndFlowNodeId(flowWorkId,flowNodeId);
    }


//    @Override
//    public ResponseDTO getList(Pageable pageable, Example<FlowTask> example, String serviceName) {
//        Page<FlowTask> page;
//        Pattern pattern = Pattern.compile(serviceName);
//        List<FlowTask> list = new ArrayList<>();
//        //是否为空 --
//        if(Validator.isEmpty(example)){
//            page = flowTaskDAO.findAll(pageable);
//            System.out.println("page:"+page.getContent());
//            List<FlowTask> flowTasks = page.getContent();
//            for (FlowTask flowTask : flowTasks) {
//                Matcher matcher = pattern.matcher(flowTask.getServiceName());
//                System.out.println(flowTask.getServiceName());
//                if(matcher.find()){
//                    System.out.println("被选中了！");
//                    list.add(flowTask);
//                }
//            }
//        }else {
//            page = flowTaskDAO.findAll(example,pageable);
//            System.out.println(page.getContent());
//            List<FlowTask> flowTasks = page.getContent();
//            for (FlowTask flowTask : flowTasks) {
//                Matcher matcher = pattern.matcher(flowTask.getServiceName());
//                System.out.println(flowTask.getServiceName());
//                if(matcher.find()){
//                    System.out.println("被选中了！");
//                    list.add(flowTask);
//                }
//            }
//        }
//        PageResultDTO<FlowTask> pageResultDTO = new PageResultDTO<>();
//        pageResultDTO.setContent(list);
//        pageResultDTO.setSize(page.getSize());
//        pageResultDTO.setTotalPages(page.getTotalPages());
//        pageResultDTO.setNumber(page.getNumber());
//        pageResultDTO.setTotalElements(page.getTotalElements());
//        return ResponseDTO.succData(pageResultDTO);
//    }


    @Override
    public PageResultDTO searchList(Pageable pageable, Example<FlowTask> example) {

        Page<FlowTask> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = flowTaskDAO.findAll(pageable);
        }else {
            page = flowTaskDAO.findAll(example,pageable);
        }
        List<FlowTask> list = page.getContent();
        PageResultDTO<FlowTask> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;


    }

//    @Override
//    public ResponseDTO getList(Pageable pageable, Example<FlowTask> example, String serviceName) {
//        Page<FlowTask> page;
//        List<FlowTask> list = new ArrayList<>();
//        System.out.println(serviceName);
//        Pattern pattern = Pattern.compile(serviceName);
//        //是否为空 --
//        if(Validator.isNotEmpty(example)&&Validator.isNotEmpty(serviceName))
//        {
//            page = flowTaskDAO.findAll(example,pageable);
//            System.out.println(page.getContent());
//            List<FlowTask> flowTasks = page.getContent();
////            list = flowTasks.stream().filter((flowTask)->
////                    flowTask.getServiceName().contains(serviceName)
////            ).collect(Collectors.toList());
////            System.out.println(flowTasks);
//            for (FlowTask flowTask : flowTasks) {
//                Matcher matcher = pattern.matcher(flowTask.getServiceName());
//                System.out.println(flowTask.getServiceName());
//                if(matcher.find()){
////                    System.out.println("被选中了！");
//                    list.add(flowTask);
//                }
//            }
//            System.out.println("1:"+list);
//
//        }else if (Validator.isEmpty(example)&&Validator.isNotEmpty(serviceName)){
//            page = flowTaskDAO.findAll(pageable);
//            List<FlowTask> flowTasks = page.getContent();
//            for (FlowTask flowTask : flowTasks) {
//                Matcher matcher = pattern.matcher(flowTask.getServiceName());
//                if(matcher.find()){
//                    System.out.println("被选中了！");
//                    list.add(flowTask);
//                }
//            }
//            System.out.println("2:"+list);
//
//        }else if (Validator.isNotEmpty(example)&&Validator.isEmpty(serviceName)){
//            page = flowTaskDAO.findAll(pageable);
//            list = page.getContent();
//            System.out.println("3:"+list);
//
//        }
//        else {
//            page = flowTaskDAO.findAll(pageable);
//            list = page.getContent();
//            System.out.println("4:"+list);
//
//        }
//        PageResultDTO<FlowTask> pageResultDTO = new PageResultDTO<>();
//        pageResultDTO.setContent(list);
//        pageResultDTO.setSize(page.getSize());
//        pageResultDTO.setTotalPages(page.getTotalPages());
//        pageResultDTO.setNumber(page.getNumber());
//        pageResultDTO.setTotalElements(page.getTotalElements());
//        return ResponseDTO.succData(pageResultDTO);
//    }

    @Override
    public Optional<FlowTask> findByFlowWorkId(String flowWorkId) {
        return flowTaskDAO.findByFlowWorkId(flowWorkId);
    }

    @Override
    public List<FlowTask> getByInitiator(String initiator) {
        return flowTaskDAO.getByInitiator(initiator);
    }

    @Override
    public List<FlowTask> getByServiceId(String id) {
        return flowTaskDAO.findByServiceId(id);
    }
}
